# -*- coding: utf-8 -*-
import json
import uuid

from scrapy import Item

from squirrel_core.commons.utils.tools import calc_str_md5, gen_rowkey

__all__ = ["ItemCheckSpiderMiddleware"]


class ItemCheckSpiderMiddleware(object):

    @classmethod
    def from_crawler(cls, crawler):
        return cls()

    def process_spider_exception(self, response, exception, spider):
        pass

    def process_spider_output(self, response, result, spider):
        for r in result:
            if isinstance(r, Item):
                if 'rowkey' not in r and 'rowkey' in r.fields:
                    r['rowkey'] = gen_rowkey(r, keys=('do_time', 'name_second'))
                if '_id' not in r and '_id' in r.fields:
                    r['_id'] = calc_str_md5(json.dumps(dict(r), ensure_ascii=False) + str(uuid.uuid4()))
            yield r
